# Author: HZT
# Date: 2023/10/27
import time
from datetime import datetime

import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By

from config.config_loader import config
from page.web_ck_dd import WebCkDd
from page.web_ck_km import WebCkKm
from page.web_ck_top import WebCkTop
from page.web_sk_jk import WebSkJk
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, login_with_session, GetLog, UtilsDriver2, click_paste_img


class TestWebCkKm:
    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        self.driver.get(config.URL)
        login_with_session('system2', self.driver)
        self.driver2 = UtilsDriver2.get_web_driver()
        self.driver2.get(config.URL)
        login_with_session('system1', self.driver2)
        # 实例化
        self.ckKm = WebCkKm(self.driver2)
        self.cKTop = WebCkTop(self.driver2)
        self.skJk = WebSkJk(self.driver)
        self.ckDd = WebCkDd(self.driver2)
        self.skTop = WebSkTop(self.driver)

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 页面数据核对无误，搜索功能无误
    def test01_ck_km(self):
        try:
            self.ckKm.ck_km_click_km()
            self.ckKm.ck_km_get_kmgl()
            # 判断交易id是否降序排列
            elements = self.ckKm.ck_km_get_kpidlbs()
            data_keys = []
            for element in elements:
                data_keys.append(int(element.text))
            sorted_data_keys = sorted(data_keys, reverse=True)
            assert data_keys == sorted_data_keys
        except Exception as e:
            self.log.error("页面排序错误，原因是：{}".format(e))
            self.ckKm.base_get_img()
            raise

    # 创建出卡订单，核对卡密信息
    def test02_ck_km(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skJk.sk_jk_click_jk()
            # 定位开关元素
            switch = self.skJk.sk_jk_get_ksjk()
            # 判断开关状态
            if switch.get_attribute("aria-hidden") == "true":
                # 开关是关闭状态，点击开关
                self.skJk.sk_jk_click_ksjk()
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            count = 1  # 发卡数
            me = '100'  # 发卡金额
            filename = "steam_card.png"  # 卡图名称
            self.ckDd.ck_dd_click_jdd()
            self.ckDd.ck_dd_ck(self.driver2, me, filename, count)
            self.ckKm.ck_km_click_km()
            self.ckKm.ck_km_get_kmgl()
            melb = self.ckKm.ck_km_get_melb()
            jyelb = self.ckKm.ck_km_get_jyelb()
            ztlb = self.ckKm.ck_km_get_ztlb()
            assert me in melb
            assert "0.00" == jyelb
            assert "Waiting for buyer" == ztlb
        except Exception as e:
            self.log.error("查看订单异常,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 点击查看卡密，核对卡片状态
    def test03_ck_km(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skJk.sk_jk_click_ckkm()
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.cKTop.ck_top_click_sx()
            srlb = self.ckKm.ck_km_get_srlb()
            jyelb = self.ckKm.ck_km_get_jyelb()
            ztlb = self.ckKm.ck_km_get_ztlb()
            assert srlb == "0.00"
            assert "880.00" == jyelb
            assert "Checking available balance" == ztlb
        except Exception as e:
            self.log.error("查看订单异常,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 点击上传余额图，核对卡片状态
    def test04_ck_km(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skJk.sk_jk_click_scye()
            element = self.skJk.sk_jk_get_yetsc()
            click_paste_img("test001.png", element, self.driver)
            time.sleep(2)
            self.skJk.sk_jk_click_yettj()
            time.sleep(1)
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.cKTop.ck_top_click_sx()
            srlb = self.ckKm.ck_km_get_srlb()
            jyelb = self.ckKm.ck_km_get_jyelb()
            ztlb = self.ckKm.ck_km_get_ztlb()
            assert srlb == "0.00"
            assert "880.00" == jyelb
            assert "Processing" == ztlb
        except Exception as e:
            self.log.error("查看订单异常,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 点击处理成功，核对卡片状态
    def test05_ck_km(self):
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.cKTop.ck_top_click_sx()
            srlb = self.ckKm.ck_km_get_srlb()
            jyelb = self.ckKm.ck_km_get_jyelb()
            ztlb = self.ckKm.ck_km_get_ztlb()
            assert srlb == "836.00"
            assert "880.00" == jyelb
            assert "Success" == ztlb
        except Exception as e:
            self.log.error("查看订单异常,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise
